        <!DOCTYPE html>
        <html>
        <head>
                <meta charset="utf-8">
        <title>quat class / vector_math Library / Dart Documentation</title>
        <link rel="stylesheet" type="text/css"
            href="../styles.css">
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="../favicon.ico">
        
        </head>
        <body data-library="vector_math" data-type="quat">
        <div class="page">
        <div class="header">
          <a href="../index.html"><div class="logo"></div></a>
          <a href="../index.html">Dart Documentation</a>
         &rsaquo; <a href="../vector_math.html">vector_math</a> &rsaquo; <a href="../vector_math/quat.html">quat</a>        <div id="search-box">
          <input type="search" name="q" id="q" autocomplete="off"
              class="search-input" placeholder="Search API">
        </div>
        
      </div>
      <div class="drop-down" id="drop-down"></div>
      
        <div class="nav">
        
</div>
<div class="content">
        <h2><strong>quat</strong>
          class
        </h2>
        
<button id="show-inherited" class="show-inherited">Hide inherited</button>
<div class="doc">
<pre class="source">
class quat {
 double x;
 double y;
 double z;
 double w;

 /// Constructs a quaternion using the raw values [x], [y], [z], and [w]
 quat.raw(this.x, this.y, this.z, this.w);
 /**
  *  Constructs a new quaternion. Behaviour depends on the types of arguments:
  *
  *  +  *([double] x,[double] y,[double] z,[double] w)* Raw values
  *  +  *([vec3] axis,[double] angle)* Rotation of [angle] degrees around [axis]
  *  +  *([quat] other)* Copy of other
  *  +  *([mat3])* Convert rotation matrix into quaternion
  *
  *
  */
 quat([dynamic a, dynamic b, dynamic c, dynamic d]) {
   x = 0.0;
   y = 0.0;
   z = 0.0;
   w = 1.0;

   if (a is num &amp;&amp; b is num &amp;&amp; c is num &amp;&amp; d is num) {
     x = a.toDouble();
     y = b.toDouble();
     z = c.toDouble();
     w = d.toDouble();
     return;
   }

   if (a is vec3 &amp;&amp; b is num) {
     setAxisAngle(a, b);
     return;
   }

   if (a is vec3) {
     x = a.x;
     y = a.y;
     z = a.z;
     w = 0.0;
     return;
   }

   if (a is quat) {
     x = a.x;
     y = a.y;
     z = a.z;
     w = a.w;
     return;
   }

   if (a is mat3) {
     double trace = a.trace();
     if (trace &gt; 0.0) {
       double s = Math.sqrt(trace + 1.0);
       w = s * 0.5;
       s = 0.5 / s;
       x = (a.col1.z - a.col2.y) * s;
       y = (a.col2.x - a.col0.z) * s;
       z = (a.col0.y - a.col1.x) * s;
     } else {
       int i = a.col0.x &lt; a.col1.y ? (a.col1.y &lt; a.col2.z ? 2 : 1) :
           (a.col0.x &lt; a.col2.z ? 2 : 0);
       int j = (i + 1) % 3;
       int k = (i + 2) % 3;
       double s = Math.sqrt(a[i][i] - a[j][j] - a[k][k] + 1.0);
       this[i] = s * 0.5;
       s = 0.5 / s;
       this[3] = (a[j][k] - a[k][j]) * s;
       this[j] = (a[i][j] + a[j][i]) * s;
       this[k] = (a[i][k] + a[k][i]) * s;
     }
   }
 }

 /// Constructs a new quaternion representing a rotation of [angle] around [axis]
 quat.axisAngle(vec3 axis, num angle) {
   setAxisAngle(axis, angle);
 }

 /// Constructs a new quaternion which is a copy of [original]
 quat.copy(quat original) {
   x = original.x;
   y = original.y;
   z = original.z;
   w = original.w;
 }

 /** Constructs a random rotation */
 quat.random(Math.Random rn) {
 // From: "Uniform Random Rotations", Ken Shoemake, Graphics Gems III,
 //       pg. 124-132
   double x0 = rn.nextDouble();
   double r1 = Math.sqrt(1.0 - x0);
   double r2 = Math.sqrt(x0);
   double t1 = Math.PI*2.0 * rn.nextDouble();
   double t2 = Math.PI*2.0 * rn.nextDouble();
   double c1 = Math.cos(t1);
   double s1 = Math.sin(t1);
   double c2 = Math.cos(t2);
   double s2 = Math.sin(t2);
   x = s1 * r1;
   y = c1 * r1;
   z = s2 * r2;
   w = c2 * r2;
 }

 /// Constructs the identity quaternion
 quat.identity() {
   x = 0.0;
   y = 0.0;
   z = 0.0;
   w = 1.0;
 }

 /** Constructs the time derivative of [q] with angular velocity [omega] */
 quat.dq(quat q, vec3 omega) {
   x = omega.x * q.w + omega.y * q.z - omega.z * q.y;
   y = omega.y * q.w + omega.z * q.x - omega.x * q.z;
   z = omega.z * q.w + omega.x * q.y - omega.y * q.x;
   w = -omega.x * q.x - omega.y * q.y - omega.z * q.z;
   x *= 0.5;
   y *= 0.5;
   z *= 0.5;
   w *= 0.5;
 }

 /// Returns a new copy of this
 quat clone() {
   return new quat.copy(this);
 }

 /// Copy [source] into [this]
 void copyFrom(quat source) {
   x = source.x;
   y = source.y;
   z = source.z;
   w = source.w;
 }

 /// Copy [this] into [target].
 void copyTo(quat target) {
   target.x = x;
   target.y = y;
   target.z = z;
   target.w = w;
 }

 /// Set quaternion with rotation of [radians] around [axis].
 void setAxisAngle(vec3 axis, num radians) {
   radians = radians.toDouble();
   double len = axis.length;
   if (len == 0.0) {
     return;
   }
   double halfSin = sin(radians * 0.5) / len;
   x = axis.x * halfSin;
   y = axis.y * halfSin;
   z = axis.z * halfSin;
   w = cos(radians * 0.5);
 }

 /** Set quaternion with rotation of [yaw], [pitch] and [roll] */
 void setEuler(num yaw, num pitch, num roll) {
   yaw = yaw.toDouble();
   pitch = pitch.toDouble();
   roll = roll.toDouble();
   double halfYaw = yaw * 0.5;
   double halfPitch = pitch * 0.5;
   double halfRoll = roll * 0.5;
   double cosYaw = Math.cos(halfYaw);
   double sinYaw = Math.sin(halfYaw);
   double cosPitch = Math.cos(halfPitch);
   double sinPitch = Math.sin(halfPitch);
   double cosRoll = Math.cos(halfRoll);
   double sinRoll = Math.sin(halfRoll);
   x = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
   y = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
   z = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
   w = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
 }

 /** Normalize [this] */
 quat normalize() {
   double l = length;
   if (l == 0.0) {
     return this;
   }
   x /= l;
   y /= l;
   z /= l;
   w /= l;
   return this;
 }

 /** Conjugate [this] */
 quat conjugate() {
   x = -x;
   y = -y;
   z = -z;
   w = w;
   return this;
 }

 /** Invert [this]  */
 quat inverse() {
   double l = 1.0 / length2;
   x = -x * l;
   y = -y * l;
   z = -z * l;
   w = w * l;
   return this;
 }

 /** Normalized copy of [this]. Optionally stored in [out]*/
 quat normalized([quat out=null]) {
   if (out == null) {
     out = new quat.copy(this);
   }
   return out.normalize();
 }

 /** Conjugated copy of [this]. Optionally stored in [out] */
 quat conjugated([quat out=null]) {
   if (out == null) {
     out = new quat.copy(this);
   }
   return out.conjugate();
 }

 /** Inverted copy of [this]. Optionally stored in [out] */
 quat inverted([quat out=null]) {
   if (out == null) {
     out = new quat.copy(this);
   }
   return out.inverse();
 }

 /** Radians of rotation */
 double get radians {
   return 2.0 * acos(w);
 }

 /** Axis of rotation */
 vec3 get axis {
     double divisor = 1.0 - (w*w);
     return new vec3(x / divisor, y / divisor, z / divisor);
 }

 /** Squared length */
 double get length2 {
   return (x*x) + (y*y) + (z*z) + (w*w);
 }

 /** Length */
 double get length {
   return Math.sqrt(length2);
 }

 /** Returns a copy of [v] rotated by quaternion. Copy optionally stored in [out] */
 vec3 rotated(vec3 v, [vec3 out=null]) {
   if (out == null) {
     out = new vec3.copy(v);
   } else {
     out.copyFrom(v);
   }
   return rotate(out);
 }

 /** Rotates [v] by [this]. Returns [v]. */
 vec3 rotate(vec3 v) {
   // conjugate(this) * [v,0] * this
   double tix = -x;
   double tiy = -y;
   double tiz = -z;
   double tiw = w;
   double tx = tiw * v.x + tix * 0.0 + tiy * v.z - tiz * v.y;
   double ty = tiw * v.y + tiy * 0.0 + tiz * v.x - tix * v.z;
   double tz = tiw * v.z + tiz * 0.0 + tix * v.y - tiy * v.x;
   double tw = tiw * 0.0 - tix * v.x - tiy * v.y - tiz * v.z;
   double result_x = tw * x + tx * w + ty * z - tz * y;
   double result_y = tw * y + ty * w + tz * x - tx * z;
   double result_z = tw * z + tz * w + tx * y - ty * x;
   v.x = result_x;
   v.y = result_y;
   v.z = result_z;
   return v;
 }

 /** Return a copy of [this] divided by [scale] */
 quat operator/(num scale) {
   scale = scale.toDouble();
   return new quat(x / scale, y / scale, z / scale, w / scale);
 }

 /**  Returns copy of [this] multiplied by [scale]
   *  Returns copy of [this] rotated by [otherQuat]
   */
 quat operator*(dynamic other) {
   if (other is num) {
     other = other.toDobule();
     return new quat(x * other, y * other, z * other, w * other);
   }
   if (other is quat) {
     return new quat(w * other.x + x * other.w + y * other.z - z * other.y,
                     w * other.y + y * other.w + z * other.x - x * other.z,
                     w * other.z + z * other.w + x * other.y - y * other.x,
                     w * other.w - x * other.x - y * other.y - z * other.z);
   }
 }

 /** Returns copy of [this] - [other] */
 quat operator+(quat other) {
   return new quat(x + other.x, y + other.y, z + other.z, w + other.w);
 }

 /** Returns copy of [this] + [other] */
 quat operator-(quat other) {
   return new quat(x - other.x, y - other.y, z - other.z, w - other.w);
 }

 /** Returns negated copy of [this] */
 quat operator-() {
   return new quat(-x, -y, -z, -w);
 }

 /** Treats [this] as an array and returns [x],[y],[z], or [w] */
 double operator[](int i) {
   assert(i &gt;= 0 &amp;&amp; i &lt; 4);
   switch (i) {
   case 0: return x;
   case 1: return y;
   case 2: return z;
   case 3: return w;
   }
   return 0.0;
 }

 /** Treats [this] as an array and assigns [x],[y],[z], or [w] the value of [arg]*/
 void operator[]=(int i, double arg) {
   assert(i &gt;= 0 &amp;&amp; i &lt; 4);
   switch (i) {
   case 0: x = arg; break;
   case 1: y = arg; break;
   case 2: z = arg; break;
   case 3: w = arg; break;
   }
 }

 /** Returns a rotation matrix containing the same rotation as [this] */
 mat3 asRotationMatrix() {
   double d = length2;
   assert(d != 0.0);
   double s = 2.0 / d;

   double xs = x * s;
   double ys = y * s;
   double zs = z * s;

   double wx = w * xs;
   double wy = w * ys;
   double wz = w * zs;

   double xx = x * xs;
   double xy = x * ys;
   double xz = x * zs;

   double yy = y * ys;
   double yz = y * zs;
   double zz = z * zs;

   return new mat3.raw(1.0 - (yy + zz), xy + wz, xz - wy, // column 0
     xy - wz, 1.0 - (xx + zz), yz + wx, // column 1
     xz + wy, yz - wx, 1.0 - (xx + yy) // column 2
     );
 }

 /** Returns a printable string */
 String toString() {
   return '$x, $y, $z @ $w';
 }

 /** Returns relative error between [this]  and [correct] */
 double relativeError(quat correct) {
   quat diff = correct - this;
   double norm_diff = diff.length;
   double correct_norm = correct.length;
   return norm_diff/correct_norm;
 }

 /** Returns absolute error between [this] and [correct] */
 double absoluteError(quat correct) {
   double this_norm = length;
   double correct_norm = correct.length;
   double norm_diff = (this_norm - correct_norm).abs();
   return norm_diff;
 }
}
</pre>
</div>
<div>
<h3>Constructors</h3>
<div class="method"><h4 id="quat">
<button class="show-code">Code</button>
new <strong>quat</strong>([a, b, c, d]) <a class="anchor-link" href="#quat"
              title="Permalink to quat.quat">#</a></h4>
<div class="doc">
<p> Constructs a new quaternion. Behaviour depends on the types of arguments:</p><ul><li><em>(<code>double</code> x,<code>double</code> y,<code>double</code> z,<code>double</code> w)</em> Raw values</li><li><em>(<a class="crossref" href="../vector_math/vec3.html">vec3</a> axis,<code>double</code> angle)</em> Rotation of <code>angle</code> degrees around <a class="crossref" href="../vector_math/quat.html#axis">axis</a></li><li><em>(<a class="crossref" href="../vector_math/quat.html#quat">quat</a> other)</em> Copy of other</li><li><em>(<a class="crossref" href="../vector_math/mat3.html">mat3</a>)</em> Convert rotation matrix into quaternion</li></ul>
<pre class="source">
quat([dynamic a, dynamic b, dynamic c, dynamic d]) {
 x = 0.0;
 y = 0.0;
 z = 0.0;
 w = 1.0;

 if (a is num &amp;&amp; b is num &amp;&amp; c is num &amp;&amp; d is num) {
   x = a.toDouble();
   y = b.toDouble();
   z = c.toDouble();
   w = d.toDouble();
   return;
 }

 if (a is vec3 &amp;&amp; b is num) {
   setAxisAngle(a, b);
   return;
 }

 if (a is vec3) {
   x = a.x;
   y = a.y;
   z = a.z;
   w = 0.0;
   return;
 }

 if (a is quat) {
   x = a.x;
   y = a.y;
   z = a.z;
   w = a.w;
   return;
 }

 if (a is mat3) {
   double trace = a.trace();
   if (trace &gt; 0.0) {
     double s = Math.sqrt(trace + 1.0);
     w = s * 0.5;
     s = 0.5 / s;
     x = (a.col1.z - a.col2.y) * s;
     y = (a.col2.x - a.col0.z) * s;
     z = (a.col0.y - a.col1.x) * s;
   } else {
     int i = a.col0.x &lt; a.col1.y ? (a.col1.y &lt; a.col2.z ? 2 : 1) :
         (a.col0.x &lt; a.col2.z ? 2 : 0);
     int j = (i + 1) % 3;
     int k = (i + 2) % 3;
     double s = Math.sqrt(a[i][i] - a[j][j] - a[k][k] + 1.0);
     this[i] = s * 0.5;
     s = 0.5 / s;
     this[3] = (a[j][k] - a[k][j]) * s;
     this[j] = (a[i][j] + a[j][i]) * s;
     this[k] = (a[i][k] + a[k][i]) * s;
   }
 }
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.axisAngle">
<button class="show-code">Code</button>
new <strong>quat.axisAngle</strong>(<a href="../vector_math/vec3.html">vec3</a> axis, num angle) <a class="anchor-link" href="#quat.axisAngle"
              title="Permalink to quat.quat.axisAngle">#</a></h4>
<div class="doc">
<p>Constructs a new quaternion representing a rotation of 
<span class="param">angle</span> around 
<span class="param">axis</span></p>
<pre class="source">
quat.axisAngle(vec3 axis, num angle) {
 setAxisAngle(axis, angle);
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.copy">
<button class="show-code">Code</button>
new <strong>quat.copy</strong>(<a href="../vector_math/quat.html">quat</a> original) <a class="anchor-link" href="#quat.copy"
              title="Permalink to quat.quat.copy">#</a></h4>
<div class="doc">
<p>Constructs a new quaternion which is a copy of 
<span class="param">original</span></p>
<pre class="source">
quat.copy(quat original) {
 x = original.x;
 y = original.y;
 z = original.z;
 w = original.w;
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.dq">
<button class="show-code">Code</button>
new <strong>quat.dq</strong>(<a href="../vector_math/quat.html">quat</a> q, <a href="../vector_math/vec3.html">vec3</a> omega) <a class="anchor-link" href="#quat.dq"
              title="Permalink to quat.quat.dq">#</a></h4>
<div class="doc">
<p>Constructs the time derivative of 
<span class="param">q</span> with angular velocity 
<span class="param">omega</span> </p>
<pre class="source">
quat.dq(quat q, vec3 omega) {
 x = omega.x * q.w + omega.y * q.z - omega.z * q.y;
 y = omega.y * q.w + omega.z * q.x - omega.x * q.z;
 z = omega.z * q.w + omega.x * q.y - omega.y * q.x;
 w = -omega.x * q.x - omega.y * q.y - omega.z * q.z;
 x *= 0.5;
 y *= 0.5;
 z *= 0.5;
 w *= 0.5;
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.identity">
<button class="show-code">Code</button>
new <strong>quat.identity</strong>() <a class="anchor-link" href="#quat.identity"
              title="Permalink to quat.quat.identity">#</a></h4>
<div class="doc">
<p>Constructs the identity quaternion</p>
<pre class="source">
quat.identity() {
 x = 0.0;
 y = 0.0;
 z = 0.0;
 w = 1.0;
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.random">
<button class="show-code">Code</button>
new <strong>quat.random</strong>(Random rn) <a class="anchor-link" href="#quat.random"
              title="Permalink to quat.quat.random">#</a></h4>
<div class="doc">
<p>Constructs a random rotation </p>
<pre class="source">
quat.random(Math.Random rn) {
// From: "Uniform Random Rotations", Ken Shoemake, Graphics Gems III,
//       pg. 124-132
 double x0 = rn.nextDouble();
 double r1 = Math.sqrt(1.0 - x0);
 double r2 = Math.sqrt(x0);
 double t1 = Math.PI*2.0 * rn.nextDouble();
 double t2 = Math.PI*2.0 * rn.nextDouble();
 double c1 = Math.cos(t1);
 double s1 = Math.sin(t1);
 double c2 = Math.cos(t2);
 double s2 = Math.sin(t2);
 x = s1 * r1;
 y = c1 * r1;
 z = s2 * r2;
 w = c2 * r2;
}
</pre>
</div>
</div>
<div class="method"><h4 id="quat.raw">
<button class="show-code">Code</button>
new <strong>quat.raw</strong>(double x, double y, double z, double w) <a class="anchor-link" href="#quat.raw"
              title="Permalink to quat.quat.raw">#</a></h4>
<div class="doc">
<p>Constructs a quaternion using the raw values 
<span class="param">x</span>, 
<span class="param">y</span>, 
<span class="param">z</span>, and 
<span class="param">w</span></p>
<pre class="source">
quat.raw(this.x, this.y, this.z, this.w);
</pre>
</div>
</div>
</div>
<div>
<h3>Properties</h3>
<div class="field"><h4 id="axis">
<button class="show-code">Code</button>
final <a href="../vector_math/vec3.html">vec3</a>         <strong>axis</strong> <a class="anchor-link"
            href="#axis"
            title="Permalink to quat.axis">#</a>
        </h4>
        <div class="doc">
<p>Axis of rotation </p>
<pre class="source">
vec3 get axis {
   double divisor = 1.0 - (w*w);
   return new vec3(x / divisor, y / divisor, z / divisor);
}
</pre>
</div>
</div>
<div class="field"><h4 id="length">
<button class="show-code">Code</button>
final double         <strong>length</strong> <a class="anchor-link"
            href="#length"
            title="Permalink to quat.length">#</a>
        </h4>
        <div class="doc">
<p>Length </p>
<pre class="source">
double get length {
 return Math.sqrt(length2);
}
</pre>
</div>
</div>
<div class="field"><h4 id="length2">
<button class="show-code">Code</button>
final double         <strong>length2</strong> <a class="anchor-link"
            href="#length2"
            title="Permalink to quat.length2">#</a>
        </h4>
        <div class="doc">
<p>Squared length </p>
<pre class="source">
double get length2 {
 return (x*x) + (y*y) + (z*z) + (w*w);
}
</pre>
</div>
</div>
<div class="field"><h4 id="radians">
<button class="show-code">Code</button>
final double         <strong>radians</strong> <a class="anchor-link"
            href="#radians"
            title="Permalink to quat.radians">#</a>
        </h4>
        <div class="doc">
<p>Radians of rotation </p>
<pre class="source">
double get radians {
 return 2.0 * acos(w);
}
</pre>
</div>
</div>
<div class="field"><h4 id="w">
<button class="show-code">Code</button>
double         <strong>w</strong> <a class="anchor-link"
            href="#w"
            title="Permalink to quat.w">#</a>
        </h4>
        <div class="doc">
<pre class="source">
w
</pre>
</div>
</div>
<div class="field"><h4 id="x">
<button class="show-code">Code</button>
double         <strong>x</strong> <a class="anchor-link"
            href="#x"
            title="Permalink to quat.x">#</a>
        </h4>
        <div class="doc">
<pre class="source">
x
</pre>
</div>
</div>
<div class="field"><h4 id="y">
<button class="show-code">Code</button>
double         <strong>y</strong> <a class="anchor-link"
            href="#y"
            title="Permalink to quat.y">#</a>
        </h4>
        <div class="doc">
<pre class="source">
y
</pre>
</div>
</div>
<div class="field"><h4 id="z">
<button class="show-code">Code</button>
double         <strong>z</strong> <a class="anchor-link"
            href="#z"
            title="Permalink to quat.z">#</a>
        </h4>
        <div class="doc">
<pre class="source">
z
</pre>
</div>
</div>
</div>
<div>
<h3>Operators</h3>
<div class="method"><h4 id="[]">
<button class="show-code">Code</button>
double <strong>operator []</strong>(int i) <a class="anchor-link" href="#[]"
              title="Permalink to quat.operator []">#</a></h4>
<div class="doc">
<p>Treats <code>this</code> as an array and returns <a class="crossref" href="../vector_math/quat.html#x">x</a>,<a class="crossref" href="../vector_math/quat.html#y">y</a>,<a class="crossref" href="../vector_math/quat.html#z">z</a>, or <a class="crossref" href="../vector_math/quat.html#w">w</a> </p>
<pre class="source">
double operator[](int i) {
 assert(i &gt;= 0 &amp;&amp; i &lt; 4);
 switch (i) {
 case 0: return x;
 case 1: return y;
 case 2: return z;
 case 3: return w;
 }
 return 0.0;
}
</pre>
</div>
</div>
<div class="method"><h4 id="[]=">
<button class="show-code">Code</button>
void <strong>operator []=</strong>(int i, double arg) <a class="anchor-link" href="#[]="
              title="Permalink to quat.operator []=">#</a></h4>
<div class="doc">
<p>Treats <code>this</code> as an array and assigns <a class="crossref" href="../vector_math/quat.html#x">x</a>,<a class="crossref" href="../vector_math/quat.html#y">y</a>,<a class="crossref" href="../vector_math/quat.html#z">z</a>, or <a class="crossref" href="../vector_math/quat.html#w">w</a> the value of 
<span class="param">arg</span></p>
<pre class="source">
void operator[]=(int i, double arg) {
 assert(i &gt;= 0 &amp;&amp; i &lt; 4);
 switch (i) {
 case 0: x = arg; break;
 case 1: y = arg; break;
 case 2: z = arg; break;
 case 3: w = arg; break;
 }
}
</pre>
</div>
</div>
<div class="method"><h4 id="+">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>operator +</strong>(<a href="../vector_math/quat.html">quat</a> other) <a class="anchor-link" href="#+"
              title="Permalink to quat.operator +">#</a></h4>
<div class="doc">
<p>Returns copy of <code>this</code> - 
<span class="param">other</span> </p>
<pre class="source">
quat operator+(quat other) {
 return new quat(x + other.x, y + other.y, z + other.z, w + other.w);
}
</pre>
</div>
</div>
<div class="method"><h4 id="unary-">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>operator -</strong>() <a class="anchor-link" href="#unary-"
              title="Permalink to quat.operator -">#</a></h4>
<div class="doc">
<p>Returns negated copy of <code>this</code> </p>
<pre class="source">
quat operator-() {
 return new quat(-x, -y, -z, -w);
}
</pre>
</div>
</div>
<div class="method"><h4 id="-">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>operator -</strong>(<a href="../vector_math/quat.html">quat</a> other) <a class="anchor-link" href="#-"
              title="Permalink to quat.operator -">#</a></h4>
<div class="doc">
<p>Returns copy of <code>this</code> + 
<span class="param">other</span> </p>
<pre class="source">
quat operator-(quat other) {
 return new quat(x - other.x, y - other.y, z - other.z, w - other.w);
}
</pre>
</div>
</div>
<div class="method"><h4 id="*">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>operator *</strong>(other) <a class="anchor-link" href="#*"
              title="Permalink to quat.operator *">#</a></h4>
<div class="doc">
<p> Returns copy of <code>this</code> multiplied by <code>scale</code>
 Returns copy of <code>this</code> rotated by <code>otherQuat</code></p>
<pre class="source">
quat operator*(dynamic other) {
 if (other is num) {
   other = other.toDobule();
   return new quat(x * other, y * other, z * other, w * other);
 }
 if (other is quat) {
   return new quat(w * other.x + x * other.w + y * other.z - z * other.y,
                   w * other.y + y * other.w + z * other.x - x * other.z,
                   w * other.z + z * other.w + x * other.y - y * other.x,
                   w * other.w - x * other.x - y * other.y - z * other.z);
 }
}
</pre>
</div>
</div>
<div class="method"><h4 id="/">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>operator /</strong>(num scale) <a class="anchor-link" href="#/"
              title="Permalink to quat.operator /">#</a></h4>
<div class="doc">
<p>Return a copy of <code>this</code> divided by 
<span class="param">scale</span> </p>
<pre class="source">
quat operator/(num scale) {
 scale = scale.toDouble();
 return new quat(x / scale, y / scale, z / scale, w / scale);
}
</pre>
</div>
</div>
</div>
<div>
<h3>Methods</h3>
<div class="method"><h4 id="absoluteError">
<button class="show-code">Code</button>
double <strong>absoluteError</strong>(<a href="../vector_math/quat.html">quat</a> correct) <a class="anchor-link" href="#absoluteError"
              title="Permalink to quat.absoluteError">#</a></h4>
<div class="doc">
<p>Returns absolute error between <code>this</code> and 
<span class="param">correct</span> </p>
<pre class="source">
double absoluteError(quat correct) {
 double this_norm = length;
 double correct_norm = correct.length;
 double norm_diff = (this_norm - correct_norm).abs();
 return norm_diff;
}
</pre>
</div>
</div>
<div class="method"><h4 id="asRotationMatrix">
<button class="show-code">Code</button>
<a href="../vector_math/mat3.html">mat3</a> <strong>asRotationMatrix</strong>() <a class="anchor-link" href="#asRotationMatrix"
              title="Permalink to quat.asRotationMatrix">#</a></h4>
<div class="doc">
<p>Returns a rotation matrix containing the same rotation as <code>this</code> </p>
<pre class="source">
mat3 asRotationMatrix() {
 double d = length2;
 assert(d != 0.0);
 double s = 2.0 / d;

 double xs = x * s;
 double ys = y * s;
 double zs = z * s;

 double wx = w * xs;
 double wy = w * ys;
 double wz = w * zs;

 double xx = x * xs;
 double xy = x * ys;
 double xz = x * zs;

 double yy = y * ys;
 double yz = y * zs;
 double zz = z * zs;

 return new mat3.raw(1.0 - (yy + zz), xy + wz, xz - wy, // column 0
   xy - wz, 1.0 - (xx + zz), yz + wx, // column 1
   xz + wy, yz - wx, 1.0 - (xx + yy) // column 2
   );
}
</pre>
</div>
</div>
<div class="method"><h4 id="clone">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>clone</strong>() <a class="anchor-link" href="#clone"
              title="Permalink to quat.clone">#</a></h4>
<div class="doc">
<p>Returns a new copy of this</p>
<pre class="source">
quat clone() {
 return new quat.copy(this);
}
</pre>
</div>
</div>
<div class="method"><h4 id="conjugate">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>conjugate</strong>() <a class="anchor-link" href="#conjugate"
              title="Permalink to quat.conjugate">#</a></h4>
<div class="doc">
<p>Conjugate <code>this</code> </p>
<pre class="source">
quat conjugate() {
 x = -x;
 y = -y;
 z = -z;
 w = w;
 return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="conjugated">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>conjugated</strong>([<a href="../vector_math/quat.html">quat</a> out = null]) <a class="anchor-link" href="#conjugated"
              title="Permalink to quat.conjugated">#</a></h4>
<div class="doc">
<p>Conjugated copy of <code>this</code>. Optionally stored in 
<span class="param">out</span> </p>
<pre class="source">
quat conjugated([quat out=null]) {
 if (out == null) {
   out = new quat.copy(this);
 }
 return out.conjugate();
}
</pre>
</div>
</div>
<div class="method"><h4 id="copyFrom">
<button class="show-code">Code</button>
void <strong>copyFrom</strong>(<a href="../vector_math/quat.html">quat</a> source) <a class="anchor-link" href="#copyFrom"
              title="Permalink to quat.copyFrom">#</a></h4>
<div class="doc">
<p>Copy 
<span class="param">source</span> into <code>this</code></p>
<pre class="source">
void copyFrom(quat source) {
 x = source.x;
 y = source.y;
 z = source.z;
 w = source.w;
}
</pre>
</div>
</div>
<div class="method"><h4 id="copyTo">
<button class="show-code">Code</button>
void <strong>copyTo</strong>(<a href="../vector_math/quat.html">quat</a> target) <a class="anchor-link" href="#copyTo"
              title="Permalink to quat.copyTo">#</a></h4>
<div class="doc">
<p>Copy <code>this</code> into 
<span class="param">target</span>.</p>
<pre class="source">
void copyTo(quat target) {
 target.x = x;
 target.y = y;
 target.z = z;
 target.w = w;
}
</pre>
</div>
</div>
<div class="method"><h4 id="inverse">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>inverse</strong>() <a class="anchor-link" href="#inverse"
              title="Permalink to quat.inverse">#</a></h4>
<div class="doc">
<p>Invert <code>this</code>  </p>
<pre class="source">
quat inverse() {
 double l = 1.0 / length2;
 x = -x * l;
 y = -y * l;
 z = -z * l;
 w = w * l;
 return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="inverted">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>inverted</strong>([<a href="../vector_math/quat.html">quat</a> out = null]) <a class="anchor-link" href="#inverted"
              title="Permalink to quat.inverted">#</a></h4>
<div class="doc">
<p>Inverted copy of <code>this</code>. Optionally stored in 
<span class="param">out</span> </p>
<pre class="source">
quat inverted([quat out=null]) {
 if (out == null) {
   out = new quat.copy(this);
 }
 return out.inverse();
}
</pre>
</div>
</div>
<div class="method"><h4 id="normalize">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>normalize</strong>() <a class="anchor-link" href="#normalize"
              title="Permalink to quat.normalize">#</a></h4>
<div class="doc">
<p>Normalize <code>this</code> </p>
<pre class="source">
quat normalize() {
 double l = length;
 if (l == 0.0) {
   return this;
 }
 x /= l;
 y /= l;
 z /= l;
 w /= l;
 return this;
}
</pre>
</div>
</div>
<div class="method"><h4 id="normalized">
<button class="show-code">Code</button>
<a href="../vector_math/quat.html">quat</a> <strong>normalized</strong>([<a href="../vector_math/quat.html">quat</a> out = null]) <a class="anchor-link" href="#normalized"
              title="Permalink to quat.normalized">#</a></h4>
<div class="doc">
<p>Normalized copy of <code>this</code>. Optionally stored in 
<span class="param">out</span></p>
<pre class="source">
quat normalized([quat out=null]) {
 if (out == null) {
   out = new quat.copy(this);
 }
 return out.normalize();
}
</pre>
</div>
</div>
<div class="method"><h4 id="relativeError">
<button class="show-code">Code</button>
double <strong>relativeError</strong>(<a href="../vector_math/quat.html">quat</a> correct) <a class="anchor-link" href="#relativeError"
              title="Permalink to quat.relativeError">#</a></h4>
<div class="doc">
<p>Returns relative error between <code>this</code>  and 
<span class="param">correct</span> </p>
<pre class="source">
double relativeError(quat correct) {
 quat diff = correct - this;
 double norm_diff = diff.length;
 double correct_norm = correct.length;
 return norm_diff/correct_norm;
}
</pre>
</div>
</div>
<div class="method"><h4 id="rotate">
<button class="show-code">Code</button>
<a href="../vector_math/vec3.html">vec3</a> <strong>rotate</strong>(<a href="../vector_math/vec3.html">vec3</a> v) <a class="anchor-link" href="#rotate"
              title="Permalink to quat.rotate">#</a></h4>
<div class="doc">
<p>Rotates 
<span class="param">v</span> by <code>this</code>. Returns 
<span class="param">v</span>. </p>
<pre class="source">
vec3 rotate(vec3 v) {
 // conjugate(this) * [v,0] * this
 double tix = -x;
 double tiy = -y;
 double tiz = -z;
 double tiw = w;
 double tx = tiw * v.x + tix * 0.0 + tiy * v.z - tiz * v.y;
 double ty = tiw * v.y + tiy * 0.0 + tiz * v.x - tix * v.z;
 double tz = tiw * v.z + tiz * 0.0 + tix * v.y - tiy * v.x;
 double tw = tiw * 0.0 - tix * v.x - tiy * v.y - tiz * v.z;
 double result_x = tw * x + tx * w + ty * z - tz * y;
 double result_y = tw * y + ty * w + tz * x - tx * z;
 double result_z = tw * z + tz * w + tx * y - ty * x;
 v.x = result_x;
 v.y = result_y;
 v.z = result_z;
 return v;
}
</pre>
</div>
</div>
<div class="method"><h4 id="rotated">
<button class="show-code">Code</button>
<a href="../vector_math/vec3.html">vec3</a> <strong>rotated</strong>(<a href="../vector_math/vec3.html">vec3</a> v, [<a href="../vector_math/vec3.html">vec3</a> out = null]) <a class="anchor-link" href="#rotated"
              title="Permalink to quat.rotated">#</a></h4>
<div class="doc">
<p>Returns a copy of 
<span class="param">v</span> rotated by quaternion. Copy optionally stored in 
<span class="param">out</span> </p>
<pre class="source">
vec3 rotated(vec3 v, [vec3 out=null]) {
 if (out == null) {
   out = new vec3.copy(v);
 } else {
   out.copyFrom(v);
 }
 return rotate(out);
}
</pre>
</div>
</div>
<div class="method"><h4 id="setAxisAngle">
<button class="show-code">Code</button>
void <strong>setAxisAngle</strong>(<a href="../vector_math/vec3.html">vec3</a> axis, num radians) <a class="anchor-link" href="#setAxisAngle"
              title="Permalink to quat.setAxisAngle">#</a></h4>
<div class="doc">
<p>Set quaternion with rotation of 
<span class="param">radians</span> around 
<span class="param">axis</span>.</p>
<pre class="source">
void setAxisAngle(vec3 axis, num radians) {
 radians = radians.toDouble();
 double len = axis.length;
 if (len == 0.0) {
   return;
 }
 double halfSin = sin(radians * 0.5) / len;
 x = axis.x * halfSin;
 y = axis.y * halfSin;
 z = axis.z * halfSin;
 w = cos(radians * 0.5);
}
</pre>
</div>
</div>
<div class="method"><h4 id="setEuler">
<button class="show-code">Code</button>
void <strong>setEuler</strong>(num yaw, num pitch, num roll) <a class="anchor-link" href="#setEuler"
              title="Permalink to quat.setEuler">#</a></h4>
<div class="doc">
<p>Set quaternion with rotation of 
<span class="param">yaw</span>, 
<span class="param">pitch</span> and 
<span class="param">roll</span> </p>
<pre class="source">
void setEuler(num yaw, num pitch, num roll) {
 yaw = yaw.toDouble();
 pitch = pitch.toDouble();
 roll = roll.toDouble();
 double halfYaw = yaw * 0.5;
 double halfPitch = pitch * 0.5;
 double halfRoll = roll * 0.5;
 double cosYaw = Math.cos(halfYaw);
 double sinYaw = Math.sin(halfYaw);
 double cosPitch = Math.cos(halfPitch);
 double sinPitch = Math.sin(halfPitch);
 double cosRoll = Math.cos(halfRoll);
 double sinRoll = Math.sin(halfRoll);
 x = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
 y = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
 z = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
 w = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
}
</pre>
</div>
</div>
<div class="method"><h4 id="toString">
<button class="show-code">Code</button>
String <strong>toString</strong>() <a class="anchor-link" href="#toString"
              title="Permalink to quat.toString">#</a></h4>
<div class="doc">
<p>Returns a printable string </p>
<pre class="source">
String toString() {
 return '$x, $y, $z @ $w';
}
</pre>
</div>
</div>
</div>
        </div>
        <div class="clear"></div>
        </div>
        <div class="footer">
          
        </div>
        <script async src="../client-live-nav.js"></script>
        </body></html>
        
